package org.team4u.demo.fsh.web;

import com.asiainfo.dao.core.Dao;
import com.asiainfo.dao.core.SimpleDao;
import com.asiainfo.dao.core.SimpleDataSource;
import com.asiainfo.sql.builder.Sql;

import javax.sql.DataSource;

/**
 * @author Jay Wu
 */
public class TestUtil {

    public static DataSource createDataSource() {
        SimpleDataSource ds = new SimpleDataSource();
        ds.setUrl("jdbc:hsqldb:mem:db");
        ds.setUsername("sa");
        ds.setPassword("");
        return ds;
    }

    public static SimpleDao createAndInitDao() {
        SimpleDao dao = new SimpleDao(createDataSource());
        initTables(dao);
        return dao;
    }

    public static void initTables(Dao dao) {
        dao.execute(new Sql().setContent("CREATE TABLE client (\n" +
                "  client_id varchar(32) NOT NULL PRIMARY KEY,\n" +
                "  name varchar(150) DEFAULT NULL\n" +
                ");"));
    }
}